//
//  PersonalCenterVC.swift
//  mySwift
//
//  Created by X-MEN on 2021/12/7.
//

import UIKit

class PersonalCenterVC: BaseViewController,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    let lineSpacing:CGFloat = 15
    let itemCount:CGFloat = 2
    var collectionView: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.edgesForExtendedLayout = []
        self.view.backgroundColor = .white
        configUi()
    }
    
    func configUi() {
        let layout = UICollectionViewFlowLayout()
        // 设置滚动的方向
        layout.scrollDirection = .vertical;
        // 行间距
        layout.minimumLineSpacing = lineSpacing
        // 设置cell之间的间距
        layout.minimumInteritemSpacing = lineSpacing;
        collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.setCollectionViewLayout(layout, animated: false)
        collectionView.backgroundColor = UIColor.clear
        collectionView.showsHorizontalScrollIndicator = false;
        collectionView.showsVerticalScrollIndicator = false;
        collectionView.isPagingEnabled = false;
        collectionView.alwaysBounceVertical = true;
        collectionView.dataSource = self;
        collectionView.delegate = self;
        collectionView.register(LabelCollectionViewCell.self, forCellWithReuseIdentifier: NSStringFromClass(LabelCollectionViewCell.self))
        self.view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make  in
            make.edges.equalToSuperview()
        }
    }

}

extension PersonalCenterVC {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 4
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell:LabelCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(LabelCollectionViewCell.self), for: indexPath) as? LabelCollectionViewCell {
            return cell;
        }
        return UICollectionViewCell()
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    }
    
    //返回item高度的方法
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let itemW = (collectionView.bounds.size.width - (itemCount + 1) * lineSpacing ) / itemCount - 0.1
        return CGSize(width: itemW, height: itemW)
    }
    
    //返回分区缩进量的方法
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: lineSpacing, bottom: 0, right: lineSpacing)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.bounds.size.width,height: 60)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize.zero
    }
}

